﻿//**************************************************
//CLASS : tcdc_mst_address
//AUTHOR : PATTARAWUT CHANNGAM & SUCHIN INPIRUT
//LAST MODIFIED : Monday, February 25, 2013
//**************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Model
{

public class tcdc_mst_address
{

     private DBUtil DB;
     public string strConn { get; set; }
     public int address_id {get;set;}
     public int user_id {get;set;}
     public string address_type {get;set;}
     public string address_no {get;set;}
     public string province {get;set;}
     public string country {get;set;}
     public string post_code {get;set;}

     public tcdc_mst_address() {
          this.DB = new DBUtil();
          DB.strConn = strConn;
     }

     public DataTable getById(string id) {

          DataTable dt = new DataTable();
          this.DB = new DBUtil();
          DB.strConn = strConn;

          string sql = "SELECT * FROM tcdc_mst_address" + 
                       " WHERE address_id = '" + id + "'";

          dt = DB.ExecuteReader(sql);

          return dt;

     }

     public DataTable getByObject(tcdc_mst_address obj) {

          DataTable dt = new DataTable();
          this.DB = new DBUtil();
          DB.strConn = strConn;

          string sql = "SELECT * FROM tcdc_mst_address " + 
                       "WHERE 1 = 1 ";

          if(obj.address_id != null && 
             obj.address_id.ToString() != null && 
             obj.address_id.ToString() != "0")
          {
               sql += "AND address_id LIKE '%" + obj.address_id + "%' ";
          }
          if(obj.user_id != null && 
             obj.user_id.ToString() != null && 
             obj.user_id.ToString() != "0")
          {
               sql += "AND user_id LIKE '%" + obj.user_id + "%' ";
          }
          if(obj.address_type != null && 
             obj.address_type.ToString() != null && 
             obj.address_type.ToString() != "0")
          {
               sql += "AND address_type LIKE '%" + obj.address_type + "%' ";
          }
          if(obj.address_no != null && 
             obj.address_no.ToString() != null && 
             obj.address_no.ToString() != "0")
          {
               sql += "AND address_no LIKE '%" + obj.address_no + "%' ";
          }
          if(obj.province != null && 
             obj.province.ToString() != null && 
             obj.province.ToString() != "0")
          {
               sql += "AND province LIKE '%" + obj.province + "%' ";
          }
          if(obj.country != null && 
             obj.country.ToString() != null && 
             obj.country.ToString() != "0")
          {
               sql += "AND country LIKE '%" + obj.country + "%' ";
          }
          if(obj.post_code != null && 
             obj.post_code.ToString() != null && 
             obj.post_code.ToString() != "0")
          {
               sql += "AND post_code LIKE '%" + obj.post_code + "%' ";
          }
          dt = DB.ExecuteReader(sql);

          return dt;

     }

     public int insertData(tcdc_mst_address obj) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[7];

          prm[0] = new SqlParameter("@user_id", SqlDbType.Int);
          if(obj.user_id != null && 
             obj.user_id.ToString() != null && 
             obj.user_id.ToString() != "0")
          {
               prm[0].Value = obj.user_id;
          }
          else
          {
               prm[0].Value = DBNull.Value;
          }

          prm[1] = new SqlParameter("@address_type", SqlDbType.NVarChar,50);
          if(obj.address_type != null && 
             obj.address_type.ToString() != null && 
             obj.address_type.ToString() != "0")
          {
               prm[1].Value = obj.address_type;
          }
          else
          {
               prm[1].Value = DBNull.Value;
          }

          prm[2] = new SqlParameter("@address_no", SqlDbType.NVarChar,250);
          if(obj.address_no != null && 
             obj.address_no.ToString() != null && 
             obj.address_no.ToString() != "0")
          {
               prm[2].Value = obj.address_no;
          }
          else
          {
               prm[2].Value = DBNull.Value;
          }

          prm[3] = new SqlParameter("@province", SqlDbType.NVarChar,50);
          if(obj.province != null && 
             obj.province.ToString() != null && 
             obj.province.ToString() != "0")
          {
               prm[3].Value = obj.province;
          }
          else
          {
               prm[3].Value = DBNull.Value;
          }

          prm[4] = new SqlParameter("@country", SqlDbType.NVarChar,50);
          if(obj.country != null && 
             obj.country.ToString() != null && 
             obj.country.ToString() != "0")
          {
               prm[4].Value = obj.country;
          }
          else
          {
               prm[4].Value = DBNull.Value;
          }

          prm[5] = new SqlParameter("@post_code", SqlDbType.NVarChar,50);
          if(obj.post_code != null && 
             obj.post_code.ToString() != null && 
             obj.post_code.ToString() != "0")
          {
               prm[5].Value = obj.post_code;
          }
          else
          {
               prm[5].Value = DBNull.Value;
          }

          prm[6] = new SqlParameter("@ReturnValue", SqlDbType.Int);
          prm[6].Direction = ParameterDirection.Output;

          DB.ExecuteNonQueryProc("sp_tcdc_mst_address_insert", prm, false);
          return (Int32)prm[6].Value;
     }

     public int updateData(tcdc_mst_address obj) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[7];

          prm[0] = new SqlParameter("@address_id", SqlDbType.Int);
          if(obj.address_id != null && 
             obj.address_id.ToString() != null && 
             obj.address_id.ToString() != "0")
          {
               prm[0].Value = obj.address_id;
          }
          else
          {
               prm[0].Value = DBNull.Value;
          }
          prm[1] = new SqlParameter("@user_id", SqlDbType.Int);
          if(obj.user_id != null && 
             obj.user_id.ToString() != null && 
             obj.user_id.ToString() != "0")
          {
               prm[1].Value = obj.user_id;
          }
          else
          {
               prm[1].Value = DBNull.Value;
          }
          prm[2] = new SqlParameter("@address_type", SqlDbType.NVarChar,50);
          if(obj.address_type != null && 
             obj.address_type.ToString() != null && 
             obj.address_type.ToString() != "0")
          {
               prm[2].Value = obj.address_type;
          }
          else
          {
               prm[2].Value = DBNull.Value;
          }
          prm[3] = new SqlParameter("@address_no", SqlDbType.NVarChar,250);
          if(obj.address_no != null && 
             obj.address_no.ToString() != null && 
             obj.address_no.ToString() != "0")
          {
               prm[3].Value = obj.address_no;
          }
          else
          {
               prm[3].Value = DBNull.Value;
          }
          prm[4] = new SqlParameter("@province", SqlDbType.NVarChar,50);
          if(obj.province != null && 
             obj.province.ToString() != null && 
             obj.province.ToString() != "0")
          {
               prm[4].Value = obj.province;
          }
          else
          {
               prm[4].Value = DBNull.Value;
          }
          prm[5] = new SqlParameter("@country", SqlDbType.NVarChar,50);
          if(obj.country != null && 
             obj.country.ToString() != null && 
             obj.country.ToString() != "0")
          {
               prm[5].Value = obj.country;
          }
          else
          {
               prm[5].Value = DBNull.Value;
          }
          prm[6] = new SqlParameter("@post_code", SqlDbType.NVarChar,50);
          if(obj.post_code != null && 
             obj.post_code.ToString() != null && 
             obj.post_code.ToString() != "0")
          {
               prm[6].Value = obj.post_code;
          }
          else
          {
               prm[6].Value = DBNull.Value;
          }

          return DB.ExecuteNonQueryProc("sp_tcdc_mst_address_update", prm, false);
     }

     public int deleteData(string id) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[1];

          prm[0] = new SqlParameter("@address_id", id);

          return DB.ExecuteNonQueryProc("sp_tcdc_mst_address_delete", prm, false);
     }
}

}

